package com.example.phpclient.content;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.example.phpclient.DB.NotesDB;
import com.example.phpclient.R;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SelectAct extends AppCompatActivity implements View.OnClickListener {
    private Button textbtn,imgbtn,videobtn ;
    private Intent intent;

    private Button backbtn,deletebtn;
    private ImageView s_img;
    private VideoView s_video;
    private TextView s_tv;
    private NotesDB notesDB;
    private SQLiteDatabase dbWriter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);
        initView();
        //删除和返回键没有问题

        deletebtn=(Button) findViewById(R.id.s_delete);
        backbtn=(Button)findViewById(R.id.s_back);

        //图片文字显示正常
        s_img=(ImageView) findViewById(R.id.s_img);
        s_video=(VideoView) findViewById(R.id.s_video);
        s_tv=(TextView)findViewById(R.id.s_tv);





        notesDB=new NotesDB(this);
        dbWriter=notesDB.getWritableDatabase();

        deletebtn.setOnClickListener(this);
        backbtn.setOnClickListener(this);

        if (getIntent().getStringExtra(NotesDB.PATH).equals("null"))
        {
            s_img.setVisibility(View.GONE);
        }else
            {
            s_img.setVisibility(View.VISIBLE);
        }

        if (getIntent().getStringExtra(NotesDB.VIDEO).equals("null"))
        {
            s_video.setVisibility(View.GONE);
        }else
            {
            s_video.setVisibility(View.VISIBLE);
        }
        s_tv.setText(getIntent().getStringExtra(NotesDB.CONTENT));
        Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra(NotesDB.PATH));

        s_img.setImageBitmap(bitmap);
        s_video.setVideoURI(Uri.parse(getIntent().getStringExtra(NotesDB.VIDEO)));
        s_video.start();


    }
    public void initView(){
        //新写入三个button为添加图片、视频、文字
        textbtn =(Button) findViewById(R.id.addtext);
        imgbtn =(Button) findViewById(R.id.addimg);
        videobtn =(Button) findViewById(R.id.addvideo);

        //button处理事件
        textbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent=new Intent(String.valueOf(AddContent.class));
                intent.putExtra("flag","1");
                startActivity(intent);
            }
        });
        imgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent=new Intent(String.valueOf(AddContent.class));
                intent.putExtra("flag","2");
                startActivity(intent);
            }
        });
        videobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent=new Intent(String.valueOf(AddContent.class));
                intent.putExtra("flag","3");
                startActivity(intent);
            }
        });


    }

    @Override
    public void onClick(View view) {
//        intent = new Intent(this,AddContent.class);
        switch (view.getId()){
            case R.id.s_delete://删除
                deleteData();
                finish();
                break;
            case R.id.s_back://返回
                finish();
                break;


//            case R.id.addtext:
//                intent.putExtra("flag","1");
//                startActivity(intent);
//                break;
//
//            case R.id.addimg:
//                intent.putExtra("flag","2");
//                startActivity(intent);
//                break;
//
//            case R.id.addvideo:
//                intent.putExtra("flag","3");
//                startActivity(intent);
//                break;
        }

    }


    public void deleteData(){
        dbWriter.delete(NotesDB.TABLE_NAME,
                NotesDB.ID + "=" + getIntent().getIntExtra(NotesDB.ID, 0),null);

    }
}
